$(document).ready(function() {
	
	/*// 用户名cookie获取
	$("input#login_user").val(localStorage.getItem("login_user"));
	// 登录密码cookie获取
	$("input#login_pw").val(localStorage.getItem("login_pw"));
	// 记住密码获取ookie获取
	var password_remember = localStorage.getItem("password_remember");
	if ( password_remember == "checked" ){
		$('#password_remember').prop('checked',true);
	} else {
		$('#password_remember').prop('checked',false);
	}*/

	// $("button#login-btn").focus();
	
	// 回车自动登录
	$("input#login_user, input#login_pw, input#phonenum, input#verifyCode, a#login-btn").on("keyup", function(e) {
		if (e.keyCode ==13) { // 回车
			$("#login-btn").trigger("click");
		}
	})

	// 回车验证码自动提交
	$('input#verifyCode').on('keyup', function (event) {
		if(event.keyCode == 13) {
			$("#verifyButton").trigger("click");
		}
	});
	
	// 登录
	$("button#login-btn").on("click", function() {
		var $that = $(this);

		if (!commomInputCheck()) {
			return;
		}

		if (document.getElementById("verifyCodeRow").style.display != "none") {
			if (!$("input#verifyCode").val().length) {
				WeKnow.errorMsg("请输入短信验证码");
				$("input#verifyCode").focus();
				return;
			} else {
				var regSms = /^\d{4}$/;
				if(!regSms.test($("input#verifyCode").val())) {
					WeKnow.errorMsg("短信验证码格式错误");
					$("input#verifyCode").focus();
					return;
				}
			}
		}

		var verifyInfo;
		if (document.getElementById("phonenumRow").style.display == "none"
			&& document.getElementById("verifyCodeRow").style.display == "none") {
			verifyInfo = '';

		} else {
			verifyInfo = {};
			verifyInfo.code = $("input#verifyCode").val();
			verifyInfo.timelimit = $that.data('timelimit');
			verifyInfo.phone = $that.data('phone');
			verifyInfo.datasign = $that.data('datasign');
			verifyInfo = JSON.stringify(verifyInfo);
		}

		// 用户名cookie保存
		// localStorage.setItem("login_user", $("input#login_user").val());
		
		// add by zhengxb 2018.7.5  start ======================================
		// 页面记住密码功能
		/*if($('#password_remember').prop('checked')){
			//do something
			localStorage.setItem("login_pw", $("input#login_pw").val());
			localStorage.setItem("password_remember", "checked");
		} else {
			localStorage.removeItem("login_pw");
			localStorage.removeItem("password_remember");
		}*/
		// add by zhengxb 2018.7.5  start ======================================

		WeKnow.loading();
		$.post(
			"invs_weknowhealth/login",
			{
				loginUser: $("input#login_user").val(),
				loginPassword: $("input#login_pw").val(),
				verifyInfo: verifyInfo,
			},
			function(data) {
				if (data.errorcode == 0) {
					$that.data = null;
					WeKnow.submit(data.toUrl);
				} else if (data.errorcode == -1) {
					WeKnow.closeLoading();
					WeKnow.errorMsg(data.errormsg);
					if(data.distrust == 1) {
						document.getElementById("phonenumRow").style.display = "";
						document.getElementById("verifyCodeRow").style.display = "";
						$("input#phonenum").focus();
					}
				} else {
					WeKnow.systemMsg('系统异常');
				}
			}
		);
	});

	//未授权IP 短信认证
	$("button#getVerifyCode").on("click", function() {

		if (!commomInputCheck()) {
			return;
		}

		var params = {loginUser: $("input#login_user").val(), loginPassword: $("input#login_pw").val(), phonenum: $("input#phonenum").val()}
		WeKnow.loading();
		$.ajax({
			type: "POST",
			url: "invs_weknowhealth/smscode_login",
			data: params,
			success: function(data) {
				if (data.errorcode == 0) {
					WeKnow.closeLoading();
					smscodeCount();
					var v = data.v;
					var $loginBtn = $('button#login-btn');
					$loginBtn.data("timelimit", v.timelimit);// 验证码过期时间
					$loginBtn.data("datasign", v.datasign);  // 数字签名
					$loginBtn.data("phone", v.phone);

				} else if (data.errorcode == -1 || data.errorcode == -2) {
					WeKnow.closeLoading();
					WeKnow.errorMsg(data.errormsg);

				} else {
					WeKnow.systemMsg('系统异常');
				}
			}
		});
	});

	function commomInputCheck() {

		if (!$("input#login_user").val().length) {
			WeKnow.errorMsg("请输入登录账户");
			$("input#login_user").focus();
			return false;
		}

		if (!$("input#login_pw").val().length) {
			WeKnow.errorMsg("请输入登录密码");
			$("input#login_pw").focus();
			return false;
		}

		if (document.getElementById("phonenumRow").style.display != "none") {
			if (!$("input#phonenum").val().length) {
				WeKnow.errorMsg("请输入认证手机号");
				$("input#phonenum").focus();
				return false;
			} else {
				var regPhone =  /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
				if(!regPhone.test($("input#phonenum").val())) {
					WeKnow.errorMsg("手机号格式错误");
					$("input#phonenum").focus();
					return false;
				}
			}
		}

		return true;
	}

	//短信计时
	function smscodeCount() {
		var element = document.getElementById("getVerifyCode");
		element.disabled = true;
		var times = 60;
		var timer = setInterval(function () {
			if (times == 0) {
				element.innerHTML = '获取验证码';
				clearInterval(timer);
				element.disabled = false;
			} else {
				element.innerHTML = times + '秒后重试';
				element.disabled = true;
				times--;
			}
		}, 1000);
	}
});